package com.rfid.reader;

import com.rfid.spec.ConnectedPICCs;
import com.rfid.spec.L0_PICC;
import com.rfid.spec.L0_PICC_HANDLE;
import com.rfid.spec.L0_RFID;
import com.rfid.spec.NetDataReport;
import com.rfid.spec.NetManager;
import com.rfid.spec.SpecConnectParams;
import com.rfid.spec.SpecInvenParamSet;
import com.rfid.spec.SpecTagAccess;
import com.rfid.spec.SpecTagTransc;
import com.rfid.spec.TDRManager;
import com.rfid.spec.TagDataReport;
import com.rfid.spec.TagOperateHandle;
import com.rfid.spec.tagaccess.SpecOutputSet;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.util.concurrent.locks.ReentrantLock;

public abstract class PCReader {
    /*
  网络
   */
    DatagramSocket client =null;
    DatagramPacket sPacket = null;
    NetManager m_NetMgr=new NetManager();
    /*****************************************/
    public int defaultInventoryTimeout = 30000;
    public int m_LastReaderErrCode = 0;
    public long m_firmVer = 0L;
    public long m_productType = 0L;
    byte connectMode = 0;
    public boolean m_trancQuickExit = false;
    SpecInvenParamSet m_DefInvenParams = new SpecInvenParamSet();
    public ReentrantLock m_syncLock = new ReentrantLock();
    TDRManager m_TDRMgr = new TDRManager();
    L0_RFID m_L0RFID = new L0_RFID();
    public long m_lastConnectTagTypeStore = 0L;
    ConnectedPICCs m_ConnPICCs = new ConnectedPICCs();

    public static PCReader create(String readerName, String strType) {
        PCReader m_reader = null;
        if ("PC104".equals(readerName))
        {
            if (strType.equals("NET"))
            {
                m_reader = new DriverPC8001Net();
            }
            else
            {
                m_reader = new DriverPC301();
            }

        }
        else if ("PC301".equals(readerName)) {
            if (strType.equals("NET"))
            {
                m_reader = new DriverPC8001Net();
            }
            else
            {
                m_reader = new DriverPC301();
            }
        }
        else if ("PC8001".equals(readerName))
        {

            if (strType.equals("NET"))
            {
                m_reader = new DriverPC8001Net();
            }
            else
            {
                m_reader =new DriverPC8001();
            }

        }
        return m_reader;
    }

    protected void finalize() throws Throwable {
        Close();
        super.finalize();
    }

    public abstract int RDR_GetCommType();

    public abstract String GetDeviceName();

    public int OpenBlueTooth(String addr) {
        return -1;
    }

    public int OpenCom(String sPath, int m_baud, String sFrame, byte addr) {
        return -1;
    }

    public int OpenNet(String remoteIp, int remotePort) {
        return -1;
    }

    public int OpenHid(String usbDes) {
        return -1;
    }

    public int OpenZTEK(int port, int baud, String frame, byte addr) {
        return -1;
    }

    public abstract int Close();

    public void ClearTDRManager() {
        if (this.m_TDRMgr.GetTagReportCount() > 0) this.m_TDRMgr.DeleteAllReports();
    }

    public void OnInventoryFinish() {
        this.m_TDRMgr.TRCMoveFirst();
    }

    public int GetTagReportCount() {
        return this.m_TDRMgr.GetTagReportCount();
    }

    public abstract int GetReaderInfor(StringBuffer paramStringBuffer);

    public abstract int PICC_Inventory(byte paramByte, byte[] paramArrayOfByte, int paramInt, SpecInvenParamSet paramSpecInvenParamSet);

    void OnInventoryPICCFound(TagDataReport pTDR) {
        this.m_TDRMgr.AddNewReport(pTDR, Boolean.valueOf(false));
    }

    public TagDataReport GetTagDataReport(byte seek) {
        TagDataReport p = null;
        switch (seek) {
            case 0:
                p = this.m_TDRMgr.TRCGetReport();
                break;
            case 1:
                this.m_TDRMgr.TRCMoveFirst();
                p = this.m_TDRMgr.TRCGetReport();
                break;
            case 2:
                this.m_TDRMgr.TRCMoveNext();
                p = this.m_TDRMgr.TRCGetReport();
                break;
            case 3:
                this.m_TDRMgr.TRCMoveLast();
                p = this.m_TDRMgr.TRCGetReport();
                break;
        }
        return p;
    }

    abstract int AirProtocolLayerInit();

    abstract int LowLevelInit();

    public int Init() {
        int iret = AirProtocolLayerInit();
        if (iret != 0) return iret;
        iret = LowLevelInit();
        if (iret != 0) return iret;
        return 0;
    }

    public int OpenRFTransmitter(byte mAntId) {
        return -8;
    }

    public int OpenRFTransmitter() {
        return -8;
    }

    public int CloseRFTransmitter() {
        return -8;
    }

    public int RFTransmitterOperate(byte opt, byte ant) {
        return -8;
    }

    public int WriteConfigBlock(int cfgno, byte[] cfgdata, int nSize, int mask) {
        return -8;
    }

    public int SaveConfigBlock(int cfgno) {
        return -8;
    }

    public int ReadCfgBlock(int cfgNo, byte[] cfgdata) {
        return -8;
    }

    public int SetRFPower(byte index) {
        return -8;
    }

    public int GetRFPower(Byte index) {
        return -8;
    }

    public abstract int LoadFactoryDefault();

    public abstract int PICC_Connect(SpecConnectParams paramSpecConnectParams, TagOperateHandle paramTagOperateHandle);

    public int PICC_DisConnect(L0_PICC_HANDLE h) {
        if (h == null) return 0;
        if (this.m_ConnPICCs.Unregister(h) == null) return -7;
        if (h.m_picc != null) ((L0_PICC) h.m_picc).Disconnect(h);
        return 0;
    }

    public abstract int PICC_Access(L0_PICC_HANDLE paramL0_PICC_HANDLE, SpecTagAccess paramSpecTagAccess);

    public abstract int OnlineCheck();

    public int GetLastReaderErrCode() {
        return this.m_LastReaderErrCode;
    }

    public int GetOverflowTime(Integer mTime) {
        return -8;
    }

    public int SetOverflowTime(int mTime) {
        return -8;
    }

    public int GetAntennaInterfaceCount() {
        return 1;
    }

    public int SetAcessAntenna(int AntennaID) {
        return -3;
    }

    public boolean IsAirProtocolSupport(long aipId) {
        return (this.m_L0RFID.GetAPL(aipId) != null);
    }

    void SetLastReaderErrCode(int error) {
        this.m_LastReaderErrCode = error;
    }

    public int GetPassingCounter(long[] inFlow, long[] outFlow) {
        return -7;
    }

    public int ResetPassingCounter(int flag) {
        return -7;
    }

    public int GetSysTime(StringBuffer strTime) {
        return -7;
    }

    public int SetSysTime(int year, int month, int day, int hour, int min, int sec) {
        return -7;
    }

    public int ReverseInOutDirection() {
        return -7;
    }

    public int BuffMode_FetchRecords(byte flags) {
        return -7;
    }

    public int SetOutput(SpecOutputSet outRecords) {
        return -7;
    }

    public int SelectAuthKey(byte keyType) {
        return -4;
    }

    public int EmptyRAMRecord() {
        return -4;
    }

    public int BuffMode_FlashEmpty() {
        return -4;
    }

    public int Login(byte[] pwd) {
        return -4;
    }

    public int EnablePasswordLogin(byte[] pwd, boolean enable) {
        return -4;
    }

    public int UpdateLoginPassword(byte[] pwd, byte[] newPwd) {
        return -4;
    }

    public int DetectTags(int[] ant, byte[][] uid, boolean[] ret) {
        return -4;
    }

    public int DetectNoise(int[] voltage, int[] bufSize) {
        return -4;
    }

    public int GetNoiseLevel(byte AntennaID, int[] min, int[] avg, int[] max) {
        return -4;
    }

    public boolean IsSupportNoiseDetect() {
        return false;
    }

    public int DetectAntennaCount(int[] mCount) {
        return -4;
    }

    public int DetectAntenna(byte option, int AntennaCount, byte[] AntennaID, byte[] rst, byte[] sta, int[] imp, int[] swr) {
        return -4;
    }

    public int PICC_Transceive(L0_PICC_HANDLE hPICC, SpecTagTransc tagTransc) {
        return -4;
    }
    /*
   网络搜索
    */
    public int Net_Inventory(){return  -8;}
    public void ClearNetManager() {
        if (this.m_NetMgr.GetTagReportCount() > 0) this.m_NetMgr.DeleteAllReports();
    }
    void OnInventoryNetFound(NetDataReport pTDR) {
        this.m_NetMgr.AddNewReport(pTDR, Boolean.valueOf(false));
    }
    public void OnInventoryNetFinish() {
        this.m_NetMgr.TRCMoveFirst();
    }

    public int GetNetReportCount() {
        return this.m_NetMgr.GetTagReportCount();
    }
    public NetDataReport GetNetDataReport(byte seek) {
        NetDataReport p = null;
        switch (seek) {
            case 0:
                p = this.m_NetMgr.TRCGetReport();
                break;
            case 1:
                this.m_TDRMgr.TRCMoveFirst();
                p = this.m_NetMgr.TRCGetReport();
                break;
            case 2:
                this.m_NetMgr.TRCMoveNext();
                p = this.m_NetMgr.TRCGetReport();
                break;
            case 3:
                this.m_NetMgr.TRCMoveLast();
                p = this.m_NetMgr.TRCGetReport();
                break;
        }
        return p;
    }

    //设置网络
    public  int SetNet(byte[] pIp,byte[] pMask,byte[] pGrateway){return  -8;}
}
